from bs4 import BeautifulSoup
import requests
num=78
chapter=1
#取前7章作为例子
for i in range(num, 85, 1): 

    base_url=r'https://www.po18bc.com/book/2/'

    url=base_url+str(i)+'.html'
    headers={'Use-Agent':'Mozilia/5.0(Windows NT 11.0;WOW64;Trident/7.0;rv:11.0) like Egde'}#发送请求
    response=requests.get(url,headers=headers)
    Response=response.text.encode('iso-8859-1').decode('gbk')
    print(response.status_code)


    soup=BeautifulSoup(Response,'html.parser')#初始化对象

    #找到小说标题
    title=soup.find('div',class_='page-header text-center').h1.text
    #找到小说内容
    contents=soup.find('div',class_='panel-body',id='htmlContent').text
    #保存

    with open(f'小说第{chapter}章.txt','w',encoding='utf-8') as f:
        f.writelines(title)
        f.writelines(contents)
    chapter=chapter+1